﻿// -----------------------------------------------------------------------
// <copyright file="SubManu.cs" company="">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace B13_Ex04_Chen_305274805_Avihoo_201249893
{
    using System;
    using System.Collections.Generic;
    using System.Text;

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public class SubMenu : MenuItem
    {
        //TO-DO if the user wants to exit 
        internal List<MenuItem> m_MoreActions = new List<MenuItem>();
        internal string m_ExitName;
        private int m_exitNumber = 0;



        internal SubMenu(string i_NameOfSubManu, string i_ExitName)
            : base(i_NameOfSubManu)
        {
            m_ExitName = i_ExitName;
        }

        internal void AddMenuItem(MenuItem i_MenuItem)
        {
            m_MoreActions.Add(i_MenuItem);
        }
        
        internal override void doSomthing()
        {
            bool isWantToExit = false;
            int usersInput;
            do 
            {
                Console.Clear();
                Console.WriteLine("{0}{1}", NameOfItem, Environment.NewLine);
                
                ShowSubManu();
                usersInput = GetUsersInput(0, m_MoreActions.Count);
                if (m_exitNumber == usersInput)
                {
                    isWantToExit = true;
                }
                else
                {
                    m_MoreActions[usersInput-1].doSomthing();
                }

            } while (isWantToExit == false);
   
            
        }
        internal int GetUsersInput(int i_MinOption,int i_MaxOption)
        {
            int userReqwestInt;

            Console.WriteLine("Insert your choice. {1}", Environment.NewLine);
            string i_userReqwest = Console.ReadLine();

            bool isNumber  = int.TryParse(i_userReqwest,out userReqwestInt);

            if (isNumber == false || userReqwestInt < i_MinOption || userReqwestInt > i_MaxOption)
            {
                Console.WriteLine("Your input is invalied enter it again!{0}", Environment.NewLine);
                userReqwestInt =  GetUsersInput(i_MinOption, i_MaxOption);
            }

            return userReqwestInt;
        }
        internal void ShowSubManu()
        {
            int countOfOptions = 0;
            Console.WriteLine("{0}. {1}{2}", countOfOptions++, m_ExitName, Environment.NewLine);
            foreach (MenuItem currentSubManu in m_MoreActions)
            {
                Console.WriteLine("{0}. {1}{2}", countOfOptions++, currentSubManu.NameOfItem, Environment.NewLine);
            }
        }
    }
}
